<!-- 新增电子签名 -->
<template>
  <div id="dialog-container">
    <el-dialog v-dialogDrag :title="'修改头像'" append-to-body :close-on-click-modal="false"
      :visible.sync="dialogVisible" @close="$emit('update:show', false)" :before-close="handleClose" :show="show" width="680px">
      <el-form :model="formData" :inline="true" label-width="90px" size="small" ref="form">
        <el-row>
          <el-col :span="24">
            <el-form-item label="头像：">
              <el-upload
                class="upload-demo"
                ref="upload"
                :action="uploadUrl"
                :on-preview="handlePreview"
                :file-list="fileList" :data="uploadData" 
                :on-success="uploadSuccess" 
                list-type="picture">
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，图片数量限制一张</div>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div> -->
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        uploadUrl: `${process.env.API_URL}user/photoEdit`,
        dialogVisible: false,
        limitNum: 0,
        formData: {
          name: '',
          userName: ''
        },
        uploadData: {
          username: this.username
        },
        isShowChoose: false,
        fileList: [],
        gobackList: []
      }
    },
    props: {
         show: {
        type: Boolean,
        default: false,
      },
      username: {
        type: String
      },
      userObject: {
          type: Object
      }
    },
    watch: {
      show() {
        this.dialogVisible = this.show;
        if(this.dialogVisible) {
              this.fileList = [];
              let file = {};
              let time = new Date().getTime();
              if(this.gobackList.length> 0) {
                file = {
                name: this.gobackList[0]['fileName'],
                url: `${process.env.API_URL}user/downloadAttach?filePath=${this.gobackList[0]['filePath']}&fileName=${this.gobackList[0]['fileName']}&cache=${time}`,
                status: 'success',
                percentage: 100,
              };
              } else {
                file = {
                name: this.userObject['avatarName'],
                url: `${process.env.API_URL}user/downloadAttach?filePath=${this.userObject['avatar']}&fileName=${this.userObject['avatarName']}&cache=${time}`,
                status: 'success',
                percentage: 100,
              };
              }
              
              this.fileList.push(file);
        }
      }
    },
    methods: {
      handleClose() { /* 关闭的时候更新状态值 */
        this.dialogVisible = false;
      },
      uploadSuccess(res, file, fileList) {
        console.log(res)
        console.log(file);
        if (res.success) {
          for(let i = 0; i < fileList.length; i++) {
            fileList.splice(i, 1);
          }
          this.gobackList = res.result;
          this.$emit('refreshEdit', this.gobackList);
        }
      },
      handlePreview(file) {
        alert(fileList.length);
      },
  }
}
</script>
